extends Control

var nowChooseItem = null 

var item_in_box_pos : Vector2 = Vector2(0,0) #物品在格子里的相对坐标 15

var EmptyInvItem=preload("res://scene/InvItem.tscn").instantiate()

var user_col_num : int = 5 #用户栏的列数 更改这个并不会影响实际列数

var box_default_color=Color(1,1,1,1)
var box_chosen_color=Color(0.72,1,1,1)

var now_chosen_item_num : int = 1 #在用户栏中选择的物品的位置 从1开始



func _ready() -> void:
	for item in $InvPanel/Items.get_children():
		item.connect("gui_input",Callable(self,"_on_gui_input").bind(item))
		
	for item in $UserPanel/Items.get_children():
		item.connect("gui_input",Callable(self,"_on_gui_input").bind(item))
	
	$DropPanel.connect("gui_input",Callable(self,"_on_drop_panel_gui_input").bind($DropPanel))
		
	switch_held_item(1)
	
	$DropPanel.visible=false
	$InvPanel.visible=false

func _input(event):
	if nowChooseItem!=null:
		nowChooseItem.position=get_local_mouse_position()
	
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_WHEEL_UP: #鼠标滚轮向上

		switch_held_item(now_chosen_item_num-1)
			
		
		print("up %d"%now_chosen_item_num)

	
	elif event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_WHEEL_DOWN: #鼠标滚轮向上

		switch_held_item(now_chosen_item_num+1)
			
		print("down %d"%now_chosen_item_num)

func switch_held_item(num:int)->int: #切换手持物品 传入物品序号 从1开始  返回选择的物品序号
	num=(num-1+user_col_num)%user_col_num+1
	for item in $UserPanel/Items.get_children():
		item.modulate=box_default_color
	var chosen_box=get_node("UserPanel/Items/Item%d"%num)
	chosen_box.modulate=box_chosen_color
	if chosen_box.get_children().size()==0:
		Global.held_item={}
	else:
		Global.held_item=chosen_box.get_children()[0]
	
	now_chosen_item_num=num
	print(Global.held_item)
	return now_chosen_item_num
	
	
#_on_gui_input和_on_input不同的是 前者只能在鼠标放在对应的面板内才能接收 后者是整个游戏
func _on_gui_input(event,itembox):
	if event is InputEventMouseButton and event.pressed and event.button_index==MOUSE_BUTTON_LEFT:
		#print("11111")
		var child=itembox.get_children()
		print(child.size())
		if child.size()==1:#如果格子有物品
			if nowChooseItem==null: #格子有物品 且鼠标上为空
				print("格子有物品 且鼠标上为空")
				nowChooseItem=child[0].deep_copy()
				nowChooseItem.position=get_local_mouse_position()
				nowChooseItem.scale=Vector2(3.4,3.4)
				get_tree().get_root().add_child(nowChooseItem)
				
				child[0].queue_free()		
				
			else: #格子有物品 且鼠标上不为空
				print("格子有物品 且鼠标上不为空")
				
				if nowChooseItem.data["name"]==child[0].data["name"] and nowChooseItem.data["stackable"]==true:
					child[0].data["ammount"]+=nowChooseItem.data["ammount"]
					child[0].refresh()
					nowChooseItem.queue_free()
					nowChooseItem=null
				else:
				
					var item=nowChooseItem.deep_copy()
					nowChooseItem.queue_free()
					nowChooseItem=child[0].deep_copy()
					nowChooseItem.position=get_local_mouse_position()
					nowChooseItem.scale=Vector2(3.4,3.4)
					get_tree().get_root().add_child(nowChooseItem)
					child[0].queue_free()
					
					item.position=item_in_box_pos
					item.scale=Vector2(1,1)
					itembox.add_child(item)
				
		elif nowChooseItem!=null:
			print("格子没物品 且鼠标上不为空")
			var item=nowChooseItem.deep_copy()
			item.position=item_in_box_pos
			item.scale=Vector2(1,1)
			
			itembox.add_child(item)
			nowChooseItem.queue_free()
			nowChooseItem=null
	
	elif event is InputEventMouseButton and event.pressed and event.button_index==MOUSE_BUTTON_RIGHT:
		
		var child=itembox.get_children()
		if child.size()==1:#如果格子有物品
			child[0].show_split_ammount_panel()
	
	
	
func _on_drop_panel_gui_input(event,itembox):
	if event is InputEventMouseButton and event.pressed and event.button_index==MOUSE_BUTTON_LEFT:
		if nowChooseItem!=null:
			DropItem(nowChooseItem)

#丢弃物品
func DropItem(ItemNode):
	if ItemNode!=null:
		print("丢弃了")
		print(ItemNode.data["name"])
		ItemNode.queue_free()
		#后续再设计一个传出的函数


#获取新的物品节点 传入名字和数量
func GetNewItem(ItemName:String,ammount:int=1):
	var newItem=EmptyInvItem.duplicate()
	newItem.SetNewItem(ItemName,ammount)
	return newItem

#传入物品节点 按次序放入空格子
func add_item_on_empty_box(newItem):
	var node_array=$InvPanel/Items.get_children()
	node_array.append_array($UserPanel/Items.get_children())
	
	var is_put_in_succeed=false
	
	for itembox in node_array:
		var child=itembox.get_children()
		if child.size()==0:
			newItem.position=item_in_box_pos
			newItem.scale=Vector2(1,1)
			itembox.add_child(newItem)
			is_put_in_succeed=true
			break
	#没位置 则丢出
	if is_put_in_succeed==false:
		DropItem(newItem)

#添加一组测试用的物品到物品栏
func _on_add_item_button_pressed() -> void:
	
	var item_names=["diamond_helmet","diamond_chestplate","diamond_leggings","diamond_boots","diamond_pickaxe","diamond"]
	
	for i in range(1,7):
		var newItem=GetNewItem(item_names[i-1],1)
		add_item_on_empty_box(newItem)

#开关物品栏按钮
func _on_open_or_close_bag_button_pressed() -> void:
	for panel in [$DropPanel, $InvPanel]:
		panel.visible=!panel.visible
	
	#启用用户栏的物品移动
	if $DropPanel.visible==true:
		for item in $UserPanel/Items.get_children():
			item.connect("gui_input",Callable(self,"_on_gui_input").bind(item))
	else:#关闭用户栏的物品移动
		for item in $UserPanel/Items.get_children():
			item.disconnect("gui_input",Callable(self,"_on_gui_input").bind(item))
